package com.ren.scale.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ren.scale.entity.ReclScaleCollection;
import com.ren.scale.entity.ReclScaleComment;
import com.ren.scale.entity.ReclScalePraise;
import com.ren.scale.entity.vo.ReclScaleQueryVo;
import com.ren.scale.entity.vo.ReclScaleVo;
import com.ren.scale.service.ReclScaleCollectionService;
import com.ren.scale.service.ReclScaleCommentService;
import com.ren.scale.service.ReclScalePraiseService;
import com.ren.scale.service.ReclScaleService;
import com.ren.utils.page.PageItem;
import com.ren.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ReclScaleFrontController
 * @Description: TODO
 * @Author: RZY
 * @DATE: 2022/7/17 20:09
 * @Version: v1.0
 */

@Api(tags = "量表前台接口")
@RestController
@RequestMapping("/recl-scale/front")
public class ReclScaleFrontController {

    @Autowired
    ReclScaleService scaleService;

    @Autowired
    ReclScalePraiseService scalePraiseService;

    @Autowired
    ReclScaleCommentService scaleCommentService;

    @Autowired
    ReclScaleCollectionService scaleCollectionService;

    @ApiOperation(value = "查询指定用户所有收藏的量表")
    @GetMapping("/scale-detail-collection/{userId}")
    public R getScaleByCollectionUserId(@PathVariable String userId) {
        List<ReclScaleVo> reclScales = scaleService.getScaleByCollectionUserId(userId);
        return R.ok()
                .data("reclScales", reclScales);
    }

    @ApiOperation(value = "查询前台量表数据分页")
    @PostMapping("/{current}/{limit}")
    public R getFrontScaleOfPage(@RequestBody(required = false) ReclScaleQueryVo reclScaleQueryVo,
                              @PathVariable long current,
                              @PathVariable long limit) {
        PageItem<ReclScaleVo> scalePageItem = scaleService.getFrontScaleOfPage(reclScaleQueryVo, current, limit);
        return R.ok()
                .data("records", scalePageItem.getPageRecords())
                .data("total", scalePageItem.getTotal());
    }

    @ApiOperation(value = "添加量表点赞")
    @PostMapping("/scale-praise")
    public R addScalePraise(@RequestBody ReclScalePraise reclScalePraise) {
        boolean res = scalePraiseService.saveOrUpdate(reclScalePraise);
        if(res) return R.ok()
                .message("感谢你的支持！");
        else return R.error()
                .message("点赞失败，请稍后再试！");
    }

    @ApiOperation(value = "添加量表评论")
    @PostMapping("/scale-comment")
    public R addScaleComment(@RequestBody ReclScaleComment reclScaleComment) {
        boolean res = scaleCommentService.saveOrUpdate(reclScaleComment);
        if(res) return R.ok()
                .message("评论成功！");
        else return R.error()
                .message("评论失败，请稍后再试！");
    }

    @ApiOperation(value = "添加量表收藏")
    @PostMapping("/scale-collection")
    public R addScaleCollection(@RequestBody ReclScaleCollection reclScaleCollection) {
        boolean res = scaleCollectionService.saveOrUpdate(reclScaleCollection);
        if(res) return R.ok()
                .message("感谢你的支持！");
        else return R.error()
                .message("收藏失败，请稍后再试！");
    }

    @ApiOperation(value = "查询指定量表的所有评论")
    @PostMapping("/scale-comment/{scaleId}")
    public R addScaleComment(@PathVariable Long scaleId) {
        List<ReclScaleComment> reclScaleComments = scaleCommentService.list(new QueryWrapper<ReclScaleComment>()
                .eq("scale_id", scaleId));
        return R.ok()
                .data("reclScaleComments", reclScaleComments);
    }
}
